package com.sjgtw.cloud.web.rainfall.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.rainfall.domain.RainfallInspection;
import com.sjgtw.cloud.web.rainfall.mapper.RainfallInspectionMapper;
import com.sjgtw.cloud.web.rainfall.service.IRainfallInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 降雨巡视Service业务层处理
 * 
 * @author lvyoufang
 * @date 2024-03-17
 */
@Service
public class RainfallInspectionServiceImpl implements IRainfallInspectionService 
{
    @Autowired
    private RainfallInspectionMapper rainfallInspectionMapper;

    @Autowired
    private RedisService redisService;


    /**
     * 查询降雨巡视
     * 
     * @param id 降雨巡视主键
     * @return 降雨巡视
     */
    @Override
    public RainfallInspection selectRainfallInspectionById(Long id)
    {
        return rainfallInspectionMapper.selectRainfallInspectionById(id);
    }

    /**
     * 查询降雨巡视列表
     * 
     * @param rainfallInspection 降雨巡视
     * @return 降雨巡视
     */
    @Override
    public List<RainfallInspection> selectRainfallInspectionList(RainfallInspection rainfallInspection)
    {
        return rainfallInspectionMapper.selectRainfallInspectionList(rainfallInspection);
    }

    /**
     * 新增降雨巡视
     * 
     * @param rainfallInspection 降雨巡视
     * @return 结果
     */
    @Override
    public int insertRainfallInspection(RainfallInspection rainfallInspection)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        rainfallInspection.setCreateUserId(user.getUserid());
        rainfallInspection.setCreateBy(user.getUsername());
        rainfallInspection.setCreateTime(DateUtils.getNowDate());
        return rainfallInspectionMapper.insertRainfallInspection(rainfallInspection);
    }

    /**
     * 修改降雨巡视
     * 
     * @param rainfallInspection 降雨巡视
     * @return 结果
     */
    @Override
    public int updateRainfallInspection(RainfallInspection rainfallInspection)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        rainfallInspection.setUpdateBy(user.getUsername());
        rainfallInspection.setUpdateTime(DateUtils.getNowDate());
        return rainfallInspectionMapper.updateRainfallInspection(rainfallInspection);
    }

    /**
     * 批量删除降雨巡视
     * 
     * @param ids 需要删除的降雨巡视主键
     * @return 结果
     */
    @Override
    public int deleteRainfallInspectionByIds(String[] ids)
    {
        return rainfallInspectionMapper.deleteRainfallInspectionByIds(ids);
    }

    /**
     * 删除降雨巡视信息
     * 
     * @param id 降雨巡视主键
     * @return 结果
     */
    @Override
    public int deleteRainfallInspectionById(Long id)
    {
        return rainfallInspectionMapper.deleteRainfallInspectionById(id);
    }

}
